<template>
  <div class="permission-container">
    <div class="app-container">
      <page-tools>
        <template #right>
          <el-button
            type="primary"
            size="small"
            @click="addPermission('1','0')"
          >
            添加权限
          </el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-card class="card-wrapper">
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="+row.type === 1" size="small" type="text" @click="addPermission('2',row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="editPermission(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermision(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="title" @close="close">
      <!-- 表单内容 -->
      <el-form ref="form" label-width="100px" :rules="formRules" :model="formData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermission,
  reqAddPermission,
  reqUpdatePermission,
  reqDelPermission,
  reqGetPermissionDetail } from '@/api'
import { transToTree } from '@/utils/index.js'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      // 效验规则
      formRules: {
        name: [
          { required: true, message: '名称必填', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '标识必填', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 关闭弹窗
    close() {
      // 清空表单
      this.$refs.form.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
      // 关闭弹窗
      this.showDialog = false
    },
    // 编辑权限
    async editPermission(id) {
      const res = await reqGetPermissionDetail(id)
      // console.log(res)
      this.formData = res.data // 回显
      this.showDialog = true // 打开弹窗
    },
    // 删除权限
    delPermision(id) {
      this.$confirm('确定删除么？', '温馨提示').then(async() => {
        await reqDelPermission(id)
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {

      })
    },
    // 获取列表
    async getList() {
      const { data } = await getPermission()
      this.list = transToTree(data, '0') // 转树状图
      // console.log(data)
    },
    // 打开弹窗
    addPermission(type, pid) {
      // 根据点击的按钮不同 ，通过参数判断是一级还是二级
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    // 添加权限
    submit() {
      // 兜底验证
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.formData.id) { // 编辑
            const res = await reqUpdatePermission(this.formData)
            console.log(res)
          } else { // 新增
            // 发送请求
            await reqAddPermission(this.formData)
          }
          // 信息提示
          this.$message.success(`${this.title}成功`)
          // 关闭弹窗
          this.close()
          // 刷新列表
          this.getList()
        } else {
          return false
        }
      })
    }

  }
}
</script>
<style lang ="scss" scoped>
::v-deep {
  .tips {
    display: none;
  }
}
.card-wrapper {
  margin-top: 10px;
}
</style>
